import java.util.Random;

public class BattleField {
	
	public static Random random = new Random();
	
	private static int rand;
	
	static Weapon MSixteen = new Weapon("M16", 1, 10, 5, 10);
	static Weapon nineMM = new Weapon("9MM", 2, 3 ,3, 3);
	static Weapon sword = new Weapon("Sword", 3, 1, 10, 20);
	
	static Weapon weapons[] = {MSixteen, nineMM, sword};
	
	static Person jason = new Person("Jason", 100, 30, 7);
	static Person jesse = new Person("Jesse", 100, 30, 7);
	static Person anthony = new Person("Anthony", 100, 20, 5);
	static Person matt = new Person("Matt", 100, 20, 2);
	static Person xin = new Person("Xin", 100, 10, 2);
	static Person kevin = new Person("Kevin", 100, 13, 2);
	static Person wil = new Person("Wil", 100, 12, 2);
	
	static Person people[] = {jason, jesse, anthony, matt, xin, kevin, wil};
	
	static Bodypart head = new Bodypart("Head", 100);
	static Bodypart neck = new Bodypart("Neck", 100);
	static Bodypart arm = new Bodypart("Arm", 2);
	static Bodypart leg = new Bodypart("Leg", 4);
	
	static Bodypart bodyparts[] = {head, neck, arm, leg};
	
	public static void setupBattle()
	{
		//Give the fighters their weapon
		for(int x = 0; x < people.length; x++)
		{
			rand = random.nextInt(25);
			if(rand <= 10)
			{
				people[x].setWeapon(MSixteen);
			}else if(rand <= 20 && rand >= 11)
			{
				people[x].setWeapon(nineMM);
			}else
			{
				people[x].setWeapon(sword);
			}
			System.out.println(people[x].getName() + " has an " + people[x].getWeaponName());
		}
	}
	
	public static void battle()
	{
		//Give the fighters a target
		for(int x = 0; x < people.length; x++)
		{
			if(!people[x].isDead)
			{
				rand = random.nextInt(people.length);
				if(!people[rand].isDead && people[x] != people[rand])
				{
					int hitChance = (new Random().nextInt(2) * people[x].accuracy) + people[x].getAWeapon().weaponAcc;
					if(hitChance > 6)
					{
						int damage = (((people[x].weapon.weaponDam + (new Random().nextInt(10) - 5)) + people[x].weapon.weaponAdv) * new Random().nextInt(4) - 1);
						if(damage <= 0)
						{
							damage = 0;
						}
						
						int bodyPart = random.nextInt();
						
						if(bodyPart <= 30)
						{
							//Hit the Leg
							people[x].doDamage(people[rand], damage, leg);
							System.out.println(people[rand].name + " attacked in the leg");
							
						}else if(bodyPart <= 55 && bodyPart >= 31)
						{
							//Hit the arms
							people[x].doDamage(people[rand], damage, arm);
							System.out.println(people[rand].name + " attacked in the arms");
						}else if(bodyPart <= 65 && bodyPart >= 56)
						{
							people[x].doDamage(people[rand], damage, neck);
							System.out.println(people[rand].name + " attacked in the neck");
							//Hit the neck
						}else if(bodyPart <= 74 && bodyPart >= 66)
						{
							people[x].doDamage(people[rand], damage, head);
							System.out.println(people[rand].name + " attacked in the head");
							//Hit the head
						}
						
						System.out.println(people[x].getName() + " attacks " + people[rand].getName() + " doing " + damage + " damage");
						if(people[rand].getHealth() <= 0)
						{
							people[rand].isDead = true;
							System.err.println(people[x].getName() + " killed " + people[rand].getName());
						}
					}else
					{
						System.out.println(people[x].getName() + " missed " + people[rand].getName());
					}
					try
					{
					Thread.sleep(1000L);
					}catch(Exception e) {}
				}
			}
		}
	}
	
	public static void main(String[] args)
	{
		setupBattle();
		while(true)
		{
			battle();
			try
			{
			//Thread.sleep(1000L);
			}catch(Exception e) {}
		}
	}
	
}
